﻿# -- Name_m.py
"""
    Copyright 2013 J. Kent Harbaugh

    This file is part of Raddel.

    Raddel is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raddel is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raddel.  If not, see <http://www.gnu.org/licenses/>.
"""



class Name_t:
    def GetName(Self):
        if Self._Names:
            return Self._Names.pop()
        if not Self._Tables:
            Self._Tables=Self._Campaign.Persist().Load(Self._Label)
            if Self._Tables==None:
                Self.calcStats()
                Self._Campaign.Persist().Save(Self._Label,Self._Tables)
        while len(Self._Names)<1024:
            name=Self.makeName()
            if len(name)>=3 and name not in Self._Names:
                Self._Names.append(name)
        name=Self._Names.pop()
        Self._Tables={}
        return name
    def __init__(Self,Campaign,NameFilename):
        Self._Campaign=Campaign
        Self._NameFilename=NameFilename
        (path,file)=os.path.split(NameFilename)
        (base,ext)=os.path.splitext(file)
        Self._Label='Name_'+base
        Self._Tables={}
        Self._Names=[]
    def calcStats(Self):
        Self._Tables={}
        file=open(Self._NameFilename,mode='rt')
        for line in file:
            for word in line.lower().split():
                idx0=' '
                idx1=' '
                idx2=' '
                idx3=' '
                for idxWord in range(len(word)+1):
                    if idxWord<len(word):
                        char=word[idxWord]
                        if not char.isalpha():
                            continue
                    else:
                        char=' '
                    if idx0 not in Self._Tables:
                        Self._Tables[idx0]={}
                    if idx1 not in Self._Tables[idx0]:
                        Self._Tables[idx0][idx1]={}
                    if idx2 not in Self._Tables[idx0][idx1]:
                        Self._Tables[idx0][idx1][idx2]={}
                    if idx3 not in Self._Tables[idx0][idx1][idx2]:
                        Self._Tables[idx0][idx1][idx2][idx3]={}
                    if char not in Self._Tables[idx0][idx1][idx2][idx3]:
                        Self._Tables[idx0][idx1][idx2][idx3][char]=1
                    else:
                        Self._Tables[idx0][idx1][idx2][idx3][char]+=1
                    idx0=idx1
                    idx1=idx2
                    idx2=idx3
                    idx3=char
        file.close()
    def makeName(Self):
        name=''
        idx0=' '
        idx1=' '
        idx2=' '
        idx3=' '
        while len(name)<16:
            table=Self._Tables[idx0][idx1][idx2][idx3]
            num=0
            for char in table:
                num+=table[char]
            rand=random.randrange(num)
            for char in table:
                if rand<table[char]:
                    if char==' ':
                        return name.title()
                    else:
                        name+=char
                        break
                else:
                    rand-=table[char]
            idx0=idx1
            idx1=idx2
            idx2=idx3
            idx3=char



import os, pickle, random



# -- Name_m.py
